<!DOCTYPE html>
<!--支付宝小程序转账列表-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="css :: css">
</head>
<link href="/js/jquery.icheck/skins/square/blue.css" rel="stylesheet">
<link href="/css/bootstrap-toggle.min.css" rel="stylesheet">
<link href="/js/jquery.icheck/skins/square/blue.css" rel="stylesheet">
<link href="/css/chosen.css" rel="stylesheet">
<body style="opacity: 1">
<div style="padding-top: 0">
    <div class="container-fluid">
        <div class="cl-mcont">
            <div class="row">
                <div class="col-md-12" style="padding:0">
                    <div class="block-flat">
                        <form class="form-horizontal group-border-dashed" id="buyCode">
                            <input type="hidden" id="productTypes" name="productTypes">
                            <input type="hidden" id="statusArray" name="statusArray">
                            <div class="form-group">
                                <div class="input-group">
                                    <select class="form-control " id="timeType" name="timeType">
                                        <option value="CREATE_TIME" selected>入库时间</option>
                                        <option value="HANDLE_TIME">处理时间</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" name="startTime"
                                           id="startTime" style="display: inline-block; width: 145px;">
                                    <input type="text" class="form-control" name="endTime"
                                           id="endTime" style="display: inline-block; width: 145px;">
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="orderId" name="orderId"
                                           placeholder="订单号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="aliOrderId" name="aliOrderId"
                                           placeholder="支付宝流水号"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="realName" name="realName"
                                           placeholder="真实姓名"/>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" id="account" name="account"
                                           placeholder="手机号码"/>
                                </div>

                                <div class="input-group">
                                    <select class="form-control " id="transferStatus" name="transferStatus">
                                        <option value="" selected>转账状态</option>
                                        <option value="SUCCESS">转账成功</option>
                                        <option value="FAILED">转账失败</option>
                                        <option value="ABNORMAL">转账异常</option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-primary" type="button" id="search"><i
                                            class="fa fa-search"></i>查询
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-success btn-operation" type="button" id="auditSuccess"
                                            onclick="updateAliProgramStatus(1)"><i
                                            class="fa fa-check-circle"></i>置成功
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-success" type="button" id="transferAgain"
                                            onclick="updateAliProgramStatus(2)"><i
                                            class="fa fa-check-circle"></i>再次转账
                                    </button>
                                </div>
                                <div class="input-group">
                                    <button class="btn btn-success" type="button" id="addRemark"
                                            onclick="updateAliProgramStatus(3)"><i
                                            class="fa fa-check-circle"></i>添加备注
                                    </button>
                                </div>

                                <div class="input-group">
                                    <span id="stopPro">
                                            <input type="checkbox" data-onstyle="danger" data-offstyle="warning"
                                                   name="include" id="include"
                                                   data-on="转账开启" data-off="转账关闭" checked data-toggle="toggle"
                                                   data-size="mini"/>
                                    </span>
                                </div>
                                <div class="input-group" id="export_Role">
                                    <button class="btn btn-default" type="button" id="export"><i
                                            class="fa fa-download"></i> 导出
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="block-flat">
                        <div class="content">
                            <div class="table-responsive">
                                <table class="table hover" id="dataTable">
                                </table>
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <!--更新备注-->
    <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="updateStatusLabel">
        <div class="modal-dialog" role="document" style="width:800px;">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="updateStatusLabel">更新备注</h4>
                </div>
                <div class="modal-body" style="min-height: 100px;">
                    <form class="form-horizontal" role="form" id="maintainForm">
                        <input type="hidden" id="rowId" name="id">
                        <input type="hidden" id="remarkLimit" name="remarkLimit">
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label for="remark" class="col-sm-4 control-label"><span
                                        class="text-danger"></span>备注:</label>
                                <div class="col-sm-7" style="width: 240px">
                                    <input type="text" class="form-control"
                                           id="remark" name="remark">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="toSaveRemark">确定</button>
                </div>
            </div>
        </div>
    </div>


</div>
<div th:include="js :: js"></div>
<script type="text/javascript" th:attr="src=@{/js/jquery.form.js}"></script>
<script type="text/javascript" th:attr="src=@{/js/jquery.parser.js}"></script>
<script type="text/javascript" src="js/jquery.icheck/icheck.min.js"></script>
<script type="text/javascript" src="/js/chosen.jquery.js?deVersion=20181411895"></script>
<script>
    $(function () {
        buttonShiro("export_Role");
        auditGet();
        //初始化隐藏按钮
        $("#manualClearingStatus").hide();
        var myDate = new Date();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        var _start = myDate.getFullYear() + '-' + month + '-' + day + " 00:00:00";
        var _end = myDate.getFullYear() + '-' + month + '-' + day + " 23:59:59";


        $("#startTime").val(_start);
        $("#endTime").val(_end);
        laydate.render({
            elem: '#startTime',
            type: 'datetime'

        });
        laydate.render({
            elem: '#endTime'
            , type: 'datetime'
        });
        $('#dataTable').bootstrapTable({
            url: '/aliProgramTransfer/queryAliProgramTransferOrder',
            height: $(window).height() - 200,
            pageSize: 100,
            pagination: true,
            pageList: [30, 50, 100],
            clickToSelect: true,
            queryParams: queryParams,
            uniqueId: 'id',
            queryParamsType: '',
            sidePagination: 'server',
            showFooter: true,
            detailView: true,
            detailFormatter: function (index, row) {
                return getLog(row.id);
            },
            responseHandler: function (res) {
                return {
                    "total": res.total,//总页数
                    "rows": res.resultList   //数据
                };
            },
            columns: [
                {
                    checkbox: true,
                    align: 'center'
                }, {
                    width: '3px',
                    align: 'center',
                    formatter: function (v, row, index) {
                        return index + 1
                    }
                },
                {
                    field: 'orderId',
                    title: '订单号',
                    width: 90, align: 'center',
                    footerFormatter: function (value) {
                        var count = 0;
                        for (var i in value) {
                            count += value[i].transferPrice;
                        }
                        return "转账金额总计：" + count.toFixed(4);
                    },
                }, {
                    field: 'aliOrderId',
                    title: '支付宝流水号',
                    width: 90, align: 'center'
                }, {
                    field: 'operatorName',
                    title: '运营商',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'productName',
                    title: '产品名称',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'face',
                    title: '面值',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'transferPrice',
                    title: '转账金额',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'aliUserId',
                    title: '卖家支付宝账号id',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'realName',
                    title: '真实姓名',
                    width: 50,
                    align: 'center'
                }, {
                    field: 'account',
                    title: '卖家手机号码',
                    width: 50,
                    align: 'center'
                },
                {
                    field: 'transferStatus',
                    title: '转账状态',
                    width: 160,
                    align: 'center',
                    formatter: function (v, row, index) {
                        return transferStatus(v);
                    }
                },
                {
                    align: 'center', field: 'createTime', title: '入库时间', formatter: function (v, o) {
                    return o.createTime != null ? o.createTime.split('.')[0] : "";
                }
                },
                {
                    align: 'center', field: 'transferTime', title: '处理时间', formatter: function (v, o) {
                    return o.transferTime != null ? o.transferTime.split('.')[0] : "";
                }
                },
                {
                    field: 'transferRemark',
                    title: '备注',
                    width: 100,
                    align: 'center'
                }
            ]
        });
        $("#search").click(function () {
            var timeAdd = new Date($("#startTime").val());
            var timeEnd = new Date($("#endTime").val());
            if (timeAdd > timeEnd) {
                layer.msg("开始时间不能大于结束时间");
                return false;
            }
            var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
            if (require > 7) {
                layer.msg("时间间隔不能超过7天");
                return;
            }
            $("#dataTable").bootstrapTable("refresh");
        });

        //下载
        $("#export").click(function () {
            var timeAdd = new Date($("#startTime").val());
            var timeEnd = new Date($("#endTime").val());
            var require = (timeEnd - timeAdd) / (1 * 24 * 60 * 60 * 1000);
            if (require > 7) {
                layer.msg("只能下载七天的数据");
                return;
            }
            window.location.href = "/aliProgramTransfer/downLoad?" + $("#buyCode").serialize()
        })
        //开启关闭
        $('#stopPro').click(function (e) {
            var status;
            e.stopPropagation();
            var remind = "";
            var isOn = $('#include').prop('checked');
            if (isOn) {
                remind = "关闭";
                status = 0
            } else {
                remind = "开启"
                status = 1
            }
            $.confirm({
                title: '确认!',
                content: '确定要' + remind + '小程序转账吗？',
                confirm: function () {
                    $.ajax({
                        type: "POST",
                        url: "/aliProgramTransfer/changeTransferFlag",
                        data: {
                            status: status
                        },
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                layer.msg(data.message);
                                auditGet();
                            } else {
                                layer.msg(data.message);
                                auditGet();
                            }
                        }
                    })
                }
            })
        })

        function auditGet() {
            $.ajax({
                type: "POST",
                url: "/aliProgramTransfer/getTransferFlag",
                success: function (data) {
                    if (data) {
                        $('#include').bootstrapToggle('on');
                    } else {
                        $('#include').bootstrapToggle('off');
                    }
                },
            });
        }


        function queryParams(params) {
            return {
                page: params.pageNumber,
                size: params.pageSize,
                startTime: $("#startTime").val(),
                endTime: $("#endTime").val(),
                transferStatus: $.trim($("#transferStatus").val()),
                realName: $.trim($("#realName").val()),
                orderId: $.trim($("#orderId").val()),
                timeType: $("#timeType").val(),
                aliOrderId: $("#aliOrderId").val(),
                account: $.trim($("#account").val())
            };
        }

        //备注保存
        $("#toSaveRemark").click(function () {
            var remarkLimit = $('#remarkLimit').val();
            if (remarkLimit != null && remarkLimit != '' && (remarkLimit.length + $('#remark').val().length) > 250) {
                layer.msg("备注超长！");
                return;
            }
            $.ajax({
                type: "POST",
                url: "/aliProgramTransfer/setRemark",
                data: {
                    id: $('#rowId').val(),
                    remark: $('#remark').val(),
                    orderType: $('#orderType').val(),
                },
                success: function (data) {
                    if (data.success) {
                        $("#dataTable").bootstrapTable("refresh");
                        layer.msg(data.message);
                        $("#myModal1").modal("hide");
                    } else {
                        layer.msg(data.message)
                    }
                },
            });
        })
    })

    //操作日志
    function getLog(id) {
        $.ajax({
            type: "post",
            url: "/finance/queryWithdrawrLog?withdrawId=" + id + "&type=ALI_TRANSFER",
            async: false,
            success: function (data) {
                o = data;
            }
        });
        var str = '';
        for (var i = 0; i < o.length; i++) {
            if (o[i].withdrawId === undefined || o[i].withdrawId === null || o[i].withdrawId === '') {
                str += ' ';
            } else {
                if (o[i].remark != '' && o[i].remark != null && o[i].remark != 'undefined') {
                    str += '<div style="float:left">' + format(o[i].operateTime) + ' ' + o[i].admin + ' 人工补结算: '
                        + statusChange(o[i].status) + o[i].remark + '</div><br/>';
                } else {
                    str += '<div style="float:left">' + format(o[i].operateTime) + ' ' + o[i].admin + ' 操作状态:'
                        + statusChange(o[i].oldStatus) + '->' + statusChange(o[i].status) + '</div><br/>';
                }
            }
        }
        return str;
    }

    //状态转换
    function statusChange(param) {
        return {0: "未转账", 1: "转账成功",2: "转账失败", 3: "转账异常"}[param];
    }

    //操作按钮
    function updateAliProgramStatus(v) {
        var rows = $('#dataTable').bootstrapTable("getSelections");
        if (rows === null || rows.length === 0) {
            layer.msg("请选择要操作的订单");
            return;
        }
        if (rows.length > 1) {
            layer.msg("仅允许单笔操作，请确认！");
            return;
        }
        //状态区分处理：1置成功 2再次转账 3添加备注
        switch (v) {
            case 1:
                dealSuccess(rows[0]);
                break;
            case 2:
                transferAgain(rows[0]);
                break;
            case 3:
                updateRemark(rows[0]);
                break;
            default:
                break;
        }
    }

    //置成功
    function dealSuccess(row) {
        if (row.transferStatus != 'FAILED' && row.transferStatus != 'ABNORMAL') {
            layer.msg("当前状态不允许修改！");
            return;
        }
        $.confirm({
            title: '确认!',
            content: '确定置成功吗？',
            confirm: function () {
                $.ajax({
                    type: "POST",
                    url: "/aliProgramTransfer/setSuccess",
                    data: {id: row.id},
                    success: function (data) {
                        if (data.success) {
                            $("#dataTable").bootstrapTable("refresh");
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message)
                        }
                    },
                });
            },
            cancel: function () {

            }
        });
    }

    //再次转账
    function transferAgain(row) {
        if (row.transferStatus != 'FAILED' && row.transferStatus != 'ABNORMAL') {
            layer.msg("当前状态不允许修改！");
            return;
        }

        $.confirm({
            title: '确认!',
            content: '确定要再次转账吗？',
            confirm: function () {
                $.ajax({
                    type: "POST",
                    url: "/aliProgramTransfer/transferAgain",
                    data: row,
                    success: function (data) {
                        if (data.success) {
                            $("#dataTable").bootstrapTable("refresh");
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message)
                        }
                    },
                });
            },
            cancel: function () {

            }
        });
    }

    //更新备注
    function updateRemark(row) {
        $("#remarkLimit").val(row.remark);
        $("#rowId").val(row.id);
        $("#remark").val("");
        $("#myModal1").modal("show");
    }
</script>
</body>
</html>
